let data = {
    pagenum: 1,
    pagesize: 2
}

function renderArticle() {
    $.ajax({
        type: "GET",
        url: "/my/article/list",
        data: data,

        success: function (res) {
            console.log(res)
            if (res.status === 0) {
                showPage(res.total)
                let str = ''
                res.data.forEach(item => {
                    str += `<tr>
                    <td>${item.title}</td>
                    <td>${item.cate_name}</td>
                    <td>${item.pub_date}</td>
                    <td>已发布</td>
                    <td>
                        <a href="./edit.html?id=${item.id}" class="layui-btn layui-btn-xs">编辑</a>
                        <button type="button" data-id="${item.id}" class="layui-btn layui-btn-xs layui-btn-danger">删除</button>
                    </td>
                </tr>`
                    $('tbody').html(str)
                });

            }
        }
    });
}
renderArticle()

function showPage(t) {
    var laypage = layui.laypage;

    //执行一个laypage实例
    laypage.render({
        elem: 'page', //注意，这里的 test1 是 ID，不用加 # 号,
        count: t, //数据总数，从服务端得到
        limit: data.pagesize,
        curr: data.pagenum,
        layout: ['count', 'prev', 'page', 'next', 'skip', 'limit'],
        limits: [2, 3, 5, 10],
        jump: function (obj, first) {
            //obj包含了当前分页的所有参数，比如：
            console.log(obj.curr); //得到当前页，以便向服务端请求对应页的数据。
            console.log(obj.limit); //得到每页显示的条数

            //首次不执行
            if (!first) {
                data.pagenum = obj.curr
                data.pagesize = obj.limit
                renderArticle()
            }
        }
    });
}
let form = layui.form
$.ajax({
    type: "GET",
    url: "/my/category/list",
    success: function (res) {
        console.log(res)
        if (res.status === 0) {
            let str = ''
            res.data.forEach(function (item) {
                str += `<option value="${item.id}">${item.name}</option>`

            })
            $('#category').append(str);
            form.render('select')

        }


    }
})
$('#btn').on('click', function (e) {
    e.preventDefault()
    let cate_id = $('#category').val()
    let state = $('#state').val()
    if (cate_id) {
        data.cate_id = cate_id

    } else {
        delete data.cate_id
    }
    if (state) {
        data.state = state

    } else {
        delete data.state
    }
    data.pagenum = 1


    renderArticle()
})
$('tbody').on('click', 'button:contains("删除")', function () {
    let id = $(this).data('id')
    $.ajax({
        type: "GET",
        url: "/my/article/delete/" + id,
        success: function (res) {
            if (res.status === 0) {
                layer.msg(res.message)
                renderArticle()


            }

        }

    })
})